/* 
 * File:   WatingRoom.cpp
 * Author: agu
 * 
 * Created on 16 de junio de 2014, 20:18
 */

#include "client_WatingRoom.h"
#include <iostream>
#include "client_Game.h"

#define LARGO 5

WatingRoom::WatingRoom() {
    this->userCreator = false;
    this->builder = Gtk::Builder::create();
    try {
        this->builder->add_from_file("gladeInterfaces/watingRoom.glade");
    } catch (const Glib::FileError& ex) {
        std::cout << "FileError: " << ex.what() << std::endl;

    } catch (const Glib::MarkupError& ex) {
        std::cout << "MarkupError: " << ex.what() << std::endl;

    } catch (const Gtk::BuilderError& ex) {
        std::cout << "BuilderError: " << ex.what() << std::endl;

    }

}

void WatingRoom::loadWidgets() {
    this->builder->get_widget("window", this->window);
    this->builder->get_widget("buttonCancel", this->buttonCancel);
    this->builder->get_widget("buttonConnect", this->buttonConnect);
    this->builder->get_widget("buttonRefresh", this->buttonRefresh);
    this->builder->get_widget("image", this->image);
    this->builder->get_widget("labelCampaign", this->labelCampaign);
    this->builder->get_widget("labelPlayJoined", this->labelPlayJoined);
    this->builder->get_widget("labelStatus", this->labelStatus);
    this->builder->get_widget("progressBar", this->progressBar);
    gameFinish = false;

}

void WatingRoom::connectWidgets() {
    this->window->signal_hide().connect(sigc::mem_fun(this, &WatingRoom::close));
    this->buttonCancel->signal_clicked().connect(sigc::mem_fun(this, &WatingRoom::cancel));
    this->buttonConnect->signal_clicked().connect(sigc::mem_fun(this, &WatingRoom::connect));
    this->buttonRefresh->signal_clicked().connect(sigc::mem_fun(this, &WatingRoom::refresh));
    this->connectionIdTimeOut = Glib::signal_timeout().connect(sigc::mem_fun(*this, &WatingRoom::onTimeout), 50);
}

WatingRoom::~WatingRoom() {
}

void WatingRoom::run() {
    this->loadWidgets();
    this->connectWidgets();
    this->loadGameInfo();
    this->window->show();
}

void WatingRoom::close() {
    this->window->close();
}

void WatingRoom::cancel() {
    this->window->hide();
    this->windowsBack->set_visible(true);
}

void WatingRoom::connect() {
    this->window->hide();
    this->send("00002|I");
    string initialTileMap = this->recv();

    Game game("Bomberman vs Plantas vs Zombies");
    game.setInitialTileMap(initialTileMap);
    game.setSocket(this->socket);
    game.setPlayerCode(this->getPlayerCode());

    game.run();
    this->window->close();
    if (!game.isRunning()) {
        gameFinish = true;
    }
}

void WatingRoom::setWindowsBack(Gtk::Window *windowBack) {
    this->windowsBack = windowBack;
}

void WatingRoom::refresh(){ 
    if (!gameFinish) {
        this->loadPlayersGameInfo();
    }

}

void WatingRoom::setSocket(Client_socket s) {
    this->socket = s;
}

bool WatingRoom::onTimeout() {
    this->refresh();
    this->refreshBar();
    return true;
}

void WatingRoom::loadGameInfo() {
    //Recuperar desde la campaña que esta en el servers
    this->labelCampaign->set_label(this->gameName.c_str());
    this->buttonConnect->set_sensitive(false);    
    this->image->set("img/menu/labels/label.png");
    this->loadPlayersGameInfo();

}

string WatingRoom::recv() {
    string largoALeer;
    if (this->socket.recibir(&largoALeer, LARGO) == -1) {        
    }

    int largoInfo = stoi(largoALeer);
    string buffer;
    if (this->socket.recibir(&buffer, largoInfo) == -1) {        
    }

    buffer.erase(0, 1);
    return buffer;
}

void WatingRoom::send(const string msj) {
    int tam = msj.size();
    this->socket.enviar(msj, tam);
}

void WatingRoom::setGameName(const string gameName) {
    this->gameName = gameName;
}

void WatingRoom::setTotalPlayers(int totalPlayers) {
    this->totalPlayers = totalPlayers;
}

void WatingRoom::setImagePath(const string imagePath) {
    this->imagePath = imagePath;
}

void WatingRoom::setPlayerCode(const string playerCode) {
    this->playerCode = playerCode;
}

string WatingRoom::getPlayerCode() {
    return this->playerCode;
}

void WatingRoom::refreshBar() {
    double newVal = progressBar->get_fraction() + 0.01;
    if (newVal > 1.0)
        newVal = 0.0;
    //Set the new value:
    progressBar->set_fraction(newVal);
}

void WatingRoom::loadPlayersGameInfo() {
    this->send("00002|P");
    //Me envian la cantidad de jugadores conectados
    string playersJoined = this->recv();
    this->labelPlayJoined->set_label(playersJoined.substr(0, 1));
    if (stoi(playersJoined.substr(0, 1)) < this->totalPlayers) {
        this->labelStatus->set_label("En espera");
    } else {
        this->labelStatus->set_label("OK");
    }
    string playersOK = playersJoined.substr(2, 2);    
    if (playersOK == string("SI") && this->userCreator) {
        this->buttonConnect->set_sensitive(true);
    } else if (playersOK == string("SI")) {
        this->connect();
    }
}

void WatingRoom::userCreatorActive() {   
    this->userCreator = true;
}

